var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();
var encryption = require('../../utils/encryption.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: 0,
    numbers: 1,
    number: 0,
    limitNumber: 0,
    id: 0,
    goodsId: 0,
    name: '',
    mechanismName: '',
    mechanismId: 0,
    retailPrice: '',
    picUrl: '',
    sum: 0,
    mobile: '',
    nickName: '',
    wallets_password_flag: false, //密码输入遮罩
    payPassword: '', //验证支付密码
    isFocus: false, //控制input 聚焦
    wallets_password: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      id: parseInt(options.id)
    });
    if (app.globalData.hasLogin == false) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    util.request(api.VoucherDetail, {
      id: that.data.id
    })
      .then(function (res) {
        that.setData({
          goodsId: res.data.goods.id,
          name: res.data.goods.name,
          mechanismName: res.data.goods.mechanismName,
          mechanismId: res.data.goods.mechanismId,
          retailPrice: res.data.goods.retailPrice,
          picUrl: res.data.goods.picUrl,
          sum: res.data.goods.retailPrice,
          number: res.data.goods.number,
          limitNumber: res.data.goods.limitNumber
        });
      });

    // 页面显示
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      this.setData({
        mobile: userInfo.mobile,
        nickName: userInfo.nickName,
        userId: userInfo.id
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.payWallet
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  cutNumber: function () {
    let that = this
    this.setData({
      numbers: (this.data.numbers - 1 > 1) ? this.data.numbers - 1 : 1
    });
    let num = that.data.numbers
    let price = that.data.retailPrice
    that.setData({
      sum: num * price
    })
    if (that.data.numbers > that.data.limitNumber) {
      wx.showModal({
        title: '提示',
        content: '一次限购' + that.data.limitNumber + '张代金券！',
        showCancel: false
      });
      that.setData({
        numbers: that.data.limitNumber,
        sum: that.data.retailPrice * that.data.limitNumber
      })
    }
    if (that.data.numbers > that.data.number) {
      wx.showModal({
        title: '提示',
        content: '库存不足，请返回修改数量！',
        showCancel: false
      });
      that.setData({
        numbers: 1,
        sum: that.data.retailPrice
      })
    }
  },
  addNumber: function () {
    let that = this
    this.setData({
      numbers: this.data.numbers + 1
    });
    let num = that.data.numbers
    let price = that.data.retailPrice
    that.setData({
      sum: num * price
    })
    if (that.data.numbers > that.data.limitNumber) {
      wx.showModal({
        title: '提示',
        content: '一次限购' + that.data.limitNumber + '张代金券！',
        showCancel: false
      });
      that.setData({
        numbers: that.data.limitNumber,
        sum: that.data.retailPrice * that.data.limitNumber
      })
    }
    if (that.data.numbers > that.data.number) {
      wx.showModal({
        title: '提示',
        content: '库存不足，请返回修改数量！',
        showCancel: false
      });
      that.setData({
        numbers: 1,
        sum: that.data.retailPrice
      })
    }
  },
  changeNumber: function (event) {
    this.setData({
      numbers: parseInt(event.detail.value) > 1 ? parseInt(event.detail.value) : 1
    });
    let that = this
    let sums = that.data.numbers * that.data.retailPrice
    that.setData({
      sum: sums
    })
    if (that.data.numbers > that.data.limitNumber) {
      wx.showModal({
        title: '提示',
        content: '一次限购' + that.data.limitNumber + '张代金券！',
        showCancel: false
      });
      that.setData({
        numbers: that.data.limitNumber,
        sum: that.data.retailPrice * that.data.limitNumber
      })
    }
    if(that.data.numbers > that.data.number){
      wx.showModal({
        title: '提示',
        content: '库存不足，请返回修改数量！',
        showCancel: false
      });
      that.setData({
        numbers: 1,
        sum: that.data.retailPrice
      })
    }
  },
  set_Focus() { //聚焦input
    console.log('isFocus', this.data.isFocus)
    this.setData({
      isFocus: true
    })
  },
  set_notFocus() { //失去焦点
    this.setData({
      isFocus: false
    })
  },

  set_wallets_password(e) { //获取钱包密码
    let that = this
    that.setData({
      wallets_password: e.detail.value
    });
    if (that.data.wallets_password.length == 6) { //密码长度6位时，自动验证钱包支付结果

      console.log("验证密码");
      that.setData({
        payPassword: that.data.wallets_password
      });

      wallet_pay(that)
      this.close_wallets_password()

    }
  },

  close_wallets_password() { //关闭钱包输入密码遮罩
    this.setData({
      isFocus: false,
      wallets_password_flag: false,
    })
  },
  pay() { //去支付
    pay(this)
  },
  payWallet: function () {
    pay(this);

  },

})

function pay(_this) {
  console.log('钱包支付')
  _this.setData({
    wallets_password: ''
  });
  _this.setData({
    wallets_password_flag: true,
    isFocus: true
  })
}
function wallet_pay(_this) {
  let that = _this;
  util.request(api.BuyVoucher, {
    userId: that.data.userId,
    goodsId: that.data.goodsId,
    mechanismId: that.data.mechanismId,
    username: that.data.nickName,
    numbers: that.data.numbers,
    unitPrice: that.data.retailPrice,
    mobile: that.data.mobile,
    orderIntegral: that.data.sum,
    actualIntegral: that.data.sum,
    password: encryption.encrypt(that.data.payPassword)
  }, 'POST')
    .then(function (res) {
      if (res.errno === 0) {
        wx.navigateTo({
          url: '/pages/payVoucherResult/payVoucherResult?status=1'
        });
      }
      else {
        wx.showModal({
          title: '提示',
          content: res.errmsg,
          showCancel: false
        });
      }      
    })
}